package com.devunion.salon.client.app.mvc.view;

import com.devunion.salon.client.Constants;
import com.devunion.salon.client.app.bean.OfficeBeanModel;
import com.devunion.salon.client.app.mvc.SalonEvents;
import com.devunion.salon.client.rpc.CompanyService;
import com.extjs.gxt.ui.client.Events;
import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.data.*;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.View;
import com.extjs.gxt.ui.client.store.GroupingStore;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.form.TimeField;
import com.extjs.gxt.ui.client.widget.grid.*;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.extjs.gxt.ui.client.widget.layout.RowData;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.google.gwt.user.client.rpc.AsyncCallback;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Timoshenko Alexander 27.02.2009 : 13:19:05
 */
public class CompanyView extends View {

    private FormBinding formBindings;

    public CompanyView(Controller controller) {
        super(controller);
    }

    protected void handleEvent(AppEvent<?> event) {
        switch (event.type) {
            case SalonEvents.COMPONENT_INIT:
                initComponent();
                break;
        }
    }

    protected void initComponent() {
        RpcProxy<ListLoadConfig, ListLoadResult<OfficeBeanModel>> rpcProxy = new RpcProxy<ListLoadConfig, ListLoadResult<OfficeBeanModel>>() {
            protected void load(ListLoadConfig groupingLoadConfig, AsyncCallback<ListLoadResult<OfficeBeanModel>> listAsyncCallback) {
                CompanyService.App.getInstance().getCompanyOffices(groupingLoadConfig, listAsyncCallback);
            }
        };
        BaseListLoader<ListLoadConfig, ListLoadResult<OfficeBeanModel>> loader =
                new BaseListLoader<ListLoadConfig, ListLoadResult<OfficeBeanModel>>(rpcProxy);
        loader.load();
        GroupingStore<OfficeBeanModel> store = new GroupingStore<OfficeBeanModel>(loader);
        store.groupBy("address.city");

        Grid<OfficeBeanModel> grid = createGrid(store);
        grid.getSelectionModel().setSelectionMode(Style.SelectionMode.SINGLE);
        grid.getSelectionModel().addListener(Events.SelectionChange, new Listener<SelectionEvent>() {
            public void handleEvent(SelectionEvent event) {
                if (event.selection.size() > 0) {
                    formBindings.bind((ModelData) event.selection.get(0));
                } else {
                    formBindings.unbind();
                }
            }
        });
        
        ContentPanel centerPanel = Registry.get(Constants.APP_CENTER_PANEL);
        centerPanel.removeAll();
        centerPanel.setLayout(new RowLayout(Style.Orientation.HORIZONTAL));
        centerPanel.add(grid, new RowData(.70, 1));
        FormPanel edit = createFormPanel(grid);
        formBindings = new FormBinding(edit, true);
        formBindings.setStore(grid.getStore());
        centerPanel.add(edit, new RowData(.30, 1));

        centerPanel.layout();

    }

    private Grid<OfficeBeanModel> createGrid(ListStore<OfficeBeanModel> store) {
        List<ColumnConfig> configs = new ArrayList<ColumnConfig>();

        configs.add(new ColumnConfig("code", "Office code", 100));
        configs.add(new ColumnConfig("address.address1", "First address", 200));
        configs.add(new ColumnConfig("address.address2", "Second address", 200));
        configs.add(new ColumnConfig("address.city", "City", 200));
        configs.add(new ColumnConfig("address.zipCode", "Zip code", 200));
        configs.add(new ColumnConfig("address.phone", "Phone", 200));
        configs.add(new ColumnConfig("address.mobilePhone", "Mobile phone", 200));
        configs.add(new ColumnConfig("address.fax", "Fax", 200));
        configs.add(new ColumnConfig("address.email", "E-mail", 200));

        final ColumnModel cm = new ColumnModel(configs);

        GroupingView view = new GroupingView();
        view.setForceFit(true);
        view.setGroupRenderer(new GridGroupRenderer() {
            public String render(GroupColumnData data) {
                String f = cm.getColumnById(data.field).getHeader();
                String l = data.models.size() == 1 ? "Office" : "Offices";
                return f + ": " + data.group + " " + l + " " + data.models.size();
            }
        });
        Grid<OfficeBeanModel> grid = new Grid<OfficeBeanModel>(store, cm);
        grid.setView(view);

        return grid;
    }

    private FormPanel createFormPanel(final Grid<OfficeBeanModel> grid) {
        final FormPanel result = new FormPanel();
        result.setLayout(new FormLayout(FormPanel.LabelAlign.LEFT));
        result.setHeaderVisible(false);
        result.setScrollMode(Style.Scroll.AUTO);

        TextField<String> companyCode = new TextField<String>();
        companyCode.setName("code");
        companyCode.setFieldLabel("Office code" + "<font color='red'>*</font>");
        companyCode.setAllowBlank(false);
        companyCode.setWidth("100%");
        result.add(companyCode);

        TextField<String> firstAddressField = new TextField<String>();
        firstAddressField.setName("address.address1");
        firstAddressField.setFieldLabel("First adddress");
        firstAddressField.setWidth("100%");
        result.add(firstAddressField);

        TextField<String> secondAddressField = new TextField<String>();
        secondAddressField.setName("address.address2");
        secondAddressField.setFieldLabel("Second address");
        secondAddressField.setWidth("100%");
        result.add(secondAddressField);

        TextField<String> city = new TextField<String>();
        city.setName("address.city");
        city.setFieldLabel("City");
        city.setWidth("100%");
        result.add(city);

        TextField<String> zipCode = new TextField<String>();
        zipCode.setName("address.zipCode");
        zipCode.setFieldLabel("Zip code");
        zipCode.setWidth("100%");
        result.add(zipCode);

        TextField<String> phone = new TextField<String>();
        phone.setName("address.phone");
        phone.setFieldLabel("Phone");
        phone.setWidth("100%");
        result.add(phone);

        TextField<String> mobile = new TextField<String>();
        mobile.setName("address.mobilePhone");
        mobile.setFieldLabel("Mobile phone");
        mobile.setWidth("100%");
        result.add(mobile);

        TextField<String> fax = new TextField<String>();
        fax.setName("address.fax");
        fax.setFieldLabel("Fax");
        fax.setWidth("100%");
        result.add(fax);

        TextField<String> email = new TextField<String>();
        email.setName("address.email");
        email.setFieldLabel("E-mail");
        email.setWidth("100%");
        result.add(email);        
        
        result.setButtonAlign(Style.HorizontalAlignment.CENTER);
        result.addButton(new Button("Reset", new SelectionListener<ButtonEvent>() {
            public void componentSelected(ButtonEvent buttonEvent) {
                grid.getStore().rejectChanges();
            }
        }));
        result.addButton(new Button("Save", new SelectionListener<ButtonEvent>() {
            public void componentSelected(ButtonEvent buttonEvent) {
                grid.getStore().commitChanges();
            }
        }));

        return result;
    }
}
